package com.bwie.spike.controller;

import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.TimeUnit;


import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;

import com.bwie.common.utils.Md5Utils;
import com.bwie.spike.conf.ExceptionConfig;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bwie.spike.entity.OrderInfoEntity;
import com.bwie.spike.service.OrderInfoService;
import com.bwie.common.utils.PageUtils;
import com.bwie.common.utils.R;



/**
 * 订单详细信息
 *
 * @author lfw
 * @email sunlightcs@gmail.com
 * @date 2022-02-21 20:32:24
 */
@RestController
@RequestMapping("spike/orderinfo")
public class OrderInfoController {

    @Autowired
    private OrderInfoService orderInfoService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * url动态化
     */
    @RequestMapping("/addUrl")
    public R Dynamicurl(@RequestParam("goodId")String goodId){
        String s = Md5Utils.md5(goodId);
        redisTemplate.opsForValue().set(s, "1",30, TimeUnit.MINUTES);
        return R.ok().put("url", s);
    }

    /**
     * 列表
     */
    @SentinelResource(value = "get",blockHandler = "handleException",blockHandlerClass = {ExceptionConfig.class})
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = orderInfoService.queryPage(params);
        return R.ok().put("page", page);
    }

    public R exceptionHandler(@RequestParam Map<String, Object> params, BlockException e){
        e.printStackTrace();
        return R.error("进行限流操作");
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		OrderInfoEntity orderInfo = orderInfoService.getById(id);
        return R.ok().put("orderInfo", orderInfo);
    }

    /**
     * 秒杀 抢订单 并未支付 Todo
     */
    @RequestMapping("/save")
    @SentinelResource(value = "spike",fallback = "handleException",fallbackClass = {ExceptionConfig.class})
    public R save(@RequestParam("user")String user,@RequestParam("good")String good,@RequestParam("url")String url){
        String s = redisTemplate.opsForValue().get(url);
        if (StringUtils.isNotBlank(s)){
            R r=orderInfoService.GrabSingle(user,good);
            return r;
        }
        return R.error("地址不存在");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody OrderInfoEntity orderInfo){
		orderInfoService.updateById(orderInfo);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		orderInfoService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
